/*
 * Galleria Picasa Plugin v 1.0
 */
(function() {

	var G = window.Galleria;
	if (typeof G == 'undefined') {
		return;
	}

	var P = G.Picasa = function() {

		this.callback = function() {
		};
		this.options = {
			max : 1000, // nombre de photos max
			// valid values :
			// 94, 110, 128, 200, 220, 288, 320, 400, 512, 576, 640,
			// 720, 800, 912, 1024, 1152, 1280, 1440, 1600
			size : '1280'
		};
	};

	P.prototype = {
		getPhotos : function(userId, albumId) {
			var args = Array.prototype.slice.call(arguments);
			this.callback = args[3] || args[2];
			if (typeof args[2] == 'object') {
				this.setOptions(args[2]);
			}

			// url pour lister les photos
			var url = "/photo/" + userId + "/" + albumId;
	
			return this._call(url, function(data) {
				var obj = {
					length : 0
				};
				var photos = data.photoList;
	
				// nombre de photo max
				var len = Math.min(this.options.max, photos.length);
	
				for ( var i = 0; i < len; i++) {
					var photo = photos[i];
	
					var img = photo.uri;
					if (this.options.size) {
						var img = img + '?imgmax=' + this.options.size;
					}
	
					var item = {
						thumb : photo.thumbnailUrl1,
						image : img,
						title : photo.name,
						description: photo.comment, 
						link: photo.uri,
						elem: photo.uri
					};
					Array.prototype.push.call(obj, item);
				}
				this.callback.call(this, obj);
			});
		},
		getAlbums : function(userId) {
			var args = Array.prototype.slice.call(arguments);
			this.callback = args[2] || args[1];
			if (typeof args[1] == 'object') {
				this.setOptions(args[1]);
			}
			
			var url = '/album/';
			if (userId) {
				url += userId + '/';
			}
			
			return this._call(url, function(data) {
				var obj = {
					length : 0
				};
				var albums = data.albumList;
	
				// nombre de photo max
				var len = Math.min(this.options.max, albums.length);
	
				for ( var i = 0; i < len; i++) {
					var album = albums[i];

					var item = {
						thumb : album.thumbnails,
						image : album.thumbnails,
						title : album.name,
						description: album.description, 
						link: '/photo/' + album.username + '/' + album.id
					};
					
					Array.prototype.push.call(obj, item);
				}
				this.callback.call(this, obj);
			});
		},
		setOptions : function(options) {
			jQuery.extend(this.options, options);
			return this;
		},
		_call : function(url, callback) {
			var scope = this;
			$.getJSON(url, function(data) {
				if (data) {
					callback.call(scope, data);
				} else {
					G.raise('No data return !');
				}
			});
			return scope;
		}
	};

	jQuery.fn.albums = function() {
	    var selector = this.selector;
	    var a = arguments;
	};
})();